package com.gok.kanmeituyun2.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.WindowManager;
import android.widget.TextView;

import com.gok.kanmeituyun2.MainActivity;
import com.gok.kanmeituyun2.R;
import com.gok.kanmeituyun2.util.file.SharedPreferencesUtil;

/**
 * @Company 工商学院
 * @Classname
 * @Description TODO 启动活动
 * @Author wbs
 * Date 2021.6.18 10:01
 * Version 1.0
 */
public class SplashActivity extends BaseActivity {
    private TextView tvTimer;
    private int recTimes = 4;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                recTimes--;
                tvTimer.setText("" + recTimes);
                if (recTimes == 0) {
                    //倒计时结束，活动跳转
                    next();
                }
            }
        }
    };

    /**
     * @return void
     * @methodName TODO 倒计时结束方法
     * @author wbs
     * @date 2021.6.18 10:08
     */
    private void next() {
        //判断登录状态
        if (sp.readLoginStatus()) {  //登录过
            startActivity(new Intent(SplashActivity.this, MainActivity.class));
        } else {
            //活动跳转
            startActivity(new Intent(SplashActivity.this, LoginActivity.class));
        }
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //去除状态栏2
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //关联布局
        setContentView(R.layout.activity_splash);

        init();

        new Thread(new MyTimer()).start();
    }

    /**
     * @return void
     * @methodName TODO 查询控件：初始化
     * @author wbs
     * @date 2021.6.18 10:00
     */
    private void init() {
        tvTimer = findViewById(R.id.tvTimer);
    }

    //实现倒计时的多线程类： extends Thread & implements Runnable
    class MyTimer implements Runnable {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (recTimes == 0) {
                    break;
                }
                //发消息
                handler.sendEmptyMessage(0);
            }
        }
    }
}